package com.bra.ui.activity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnDismissListener;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bra.R;
import com.bra.request.UrlConstants;
import com.bra.ui.base.BaseActivity;
import com.bra.utils.CityDate;
import com.bra.utils.Contacts;
import com.bra.utils.DialogUtil;
import com.bra.utils.SPHelper;
import com.bra.utils.StringHelper;
import com.bra.utils.ToastUtil;
import com.bra.view.CityPickerDialog;

/**
 * 注册界面
 * 
 * @author lenovo
 *
 */
public class RegistActivity extends BaseActivity {
	private TextView activity_title, r_huoquma;
	private ImageView activity_back;
	private Button zhuce_btu;
	private EditText r_et_phone;
	private String PhoneNumber;
	private String key = "rjgqpmviezskehml";
	private EditText r_edithuoquma, r_editpw, r_edit_corfim_pw;
	private TimeCount time;
	private String corfim_pw, yanzhengma, password;
	private String RegistUrl = UrlConstants.IP;
	private RequestQueue mRequestQueue;
	

	@Override
	protected View onCreateView(LayoutInflater inflater,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.activity_register, null);
	}

	@Override
	protected void onViewCreated(Bundle savedInstanceState) {
		mRequestQueue = Volley.newRequestQueue(this);
		activity_title = (TextView) findViewById(R.id.activity_title);
		activity_title.setText("注册");
		activity_back = (ImageView) findViewById(R.id.activity_back);
		zhuce_btu = (Button) findViewById(R.id.zhuce_btu);
		r_et_phone = (EditText) findViewById(R.id.r_et_phone);
		r_huoquma = (TextView) findViewById(R.id.r_huoquma);
		r_edithuoquma = (EditText) findViewById(R.id.r_edithuoquma);
		r_editpw = (EditText) findViewById(R.id.r_editpw);
		r_edit_corfim_pw = (EditText) findViewById(R.id.r_edit_corfim_pw);
		time = new TimeCount(10000, 1000);// 对（10秒）
	}

	@Override
	protected void setListener() {
		activity_back.setOnClickListener(click);
		zhuce_btu.setOnClickListener(click);
		r_huoquma.setOnClickListener(click);
	}	
	
	public View.OnClickListener click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.activity_back:
				finish();
				break;
			case R.id.zhuce_btu:
				yanzhengma = r_edithuoquma.getText().toString().trim();
				password = r_editpw.getText().toString().trim();
				corfim_pw = r_edit_corfim_pw.getText().toString().trim();
				PhoneNumber = r_et_phone.getText().toString().trim();
			/*	if (StringHelper.isEmpty(PhoneNumber)) {
					ToastUtil.showToast(Regist_Activity.this, "请输入手机号");
					return;
				}
				if (!StringHelper.isMobileNO(PhoneNumber)) {
					ToastUtil.showToast(Regist_Activity.this, "手机号不合法");
					return;
				}
				if (StringHelper.isEmpty(yanzhengma)) {
					ToastUtil.showToast(Regist_Activity.this, "请输入验证码");
					return;
				}
				if (StringHelper.isEmpty(password)) {
					ToastUtil.showToast(Regist_Activity.this, "请输入密码");
					return;
				}
				if (StringHelper.isEmpty(corfim_pw)) {
					ToastUtil.showToast(Regist_Activity.this, "请再次确认密码");
					return;
				}
				if (password.length() < 6) {
					ToastUtil.showToast(Regist_Activity.this, "密码(6-16位)");
					return;
				}
				if (!password.equals(corfim_pw)) {
					ToastUtil.showToast(Regist_Activity.this, "两次密码不一致");
					return;
				}
				*/
				httpRequestRegistInfo(RegistActivity.this, RegistUrl,getRequestParams(PhoneNumber,yanzhengma,password));		
			//	goActivity(PersionInfoActivity.class);
				break;
			case R.id.r_huoquma:
				time.start();// 开始计时
				httpRequestSmsInfo(RegistActivity.this, RegistUrl,getRequestSmsParams(PhoneNumber));
				break;
			default:
				break;
			}

		}
	};

	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);// 参数依次为总时长,和计时的时间间隔
		}

		@Override
		public void onFinish() {// 计时完毕时触发
			r_huoquma.setText("获取验证码");
			r_huoquma.setClickable(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {// 计时过程显示
			r_huoquma.setClickable(false);
			r_huoquma.setText(millisUntilFinished / 1000 + "s");
		}
	}
	private void httpRequestRegistInfo(Context context, String url,final HashMap<String, String> params) {		
		 StringRequest  req = new StringRequest(Request.Method.POST, url,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						try {
							System.out.println(response.toString() + "===");											
							JSONObject Object=new JSONObject(response.toString());
							String userId=Object.getString("userId");
							String token=Object.getString("token");
							SPHelper.saveString(getApplicationContext(), "userId", userId);
							SPHelper.saveString(getApplicationContext(), "token", token);						
							goActivity(PersionMessageActivity.class);
							return;
						} catch (Exception e) {
							e.printStackTrace();
						}					
						Toast.makeText(getApplicationContext(), "数据异常，请检查网络", 0).show();
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError volleyError) {					
						Toast.makeText(getApplicationContext(), "访问失败，请检查网络", 0).show();
					}
				}) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				Map<String, String> map = params;		
				return map;
			}
		};
		mRequestQueue.add(req);
	}
	private void httpRequestSmsInfo(Context context, String url,final HashMap<String, String> params) {		
		 StringRequest  req = new StringRequest(Request.Method.POST, url,
				new Response.Listener<String>() {
					@Override
					public void onResponse(String response) {
						try {
							System.out.println(response.toString() + "===");							
							Toast.makeText(getApplicationContext(),response.toString(), 0).show();
							return;
						} catch (Exception e) {
							e.printStackTrace();
						}					
						Toast.makeText(getApplicationContext(), "数据异常，请检查网络", 0)
								.show();
					}
				}, new Response.ErrorListener() {
					@Override
					public void onErrorResponse(VolleyError volleyError) {					
						Toast.makeText(getApplicationContext(), "访问失败，请检查网络", 0).show();
					}
				}) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				Map<String, String> map = params;		
				return map;
			}
		};
		mRequestQueue.add(req);
	}
	//注册参数
private HashMap<String, String> getRequestParams(String phone, String yanzhengma,  String password) {
	HashMap<String, String> map = new HashMap<String, String>();
	map.put("userId", "");
	map.put("equipment", "android");
	map.put("requestType", "Register_Api");
	map.put("token", "");
	map.put("mobile", phone);
	map.put("password", password);			
	map.put("mobileCode", yanzhengma);// 验证码
	return map;
}
//获取验证码的参数
private HashMap<String, String> getRequestSmsParams(String phone) {
	HashMap<String, String> map = new HashMap<String, String>();
	map.put("userId", "");
	map.put("equipment", "android");
	map.put("requestType", "Sms_Api");
	map.put("token", "");
	map.put("mobile", phone);	
	return map;
}
}
